import $http from '../http'
import $fn from '../fn'
import $store from './storage'
import $toast from './toast'
const Index = {
    getParam( query, ask ){
        const { id } = $store.getUser()
        const ask_str = ask ? '?' : ''
        if($fn.hasObject(query)){
            query.share_id = id
            query = $http.serialize(query, null, 0)
        }
        if( query ) query = ask_str + query
        return query
    },
    async msg(_this, content){
        // const { from, target, webViewUrl } = e
        let { title, img, path, query={}, api='share', param, q } = content || {}
        const type = 1
        if(!img){
            const { id } = $store.getUser()
            const d = await $http.pull(null, api, { param: { type, ...param } })
            if( d == null ){
                $toast.error('分享被禁用')
                return null
            }
            title = d.title
            img = d.image
            path = d.path ? d.path : '/' + _this.route
            if(d.query) path = path + '?' + d.query + '&path=' + path
            if(id) path = path + '&share_id=' + id
            if(q) path = path + '&' + q
        }else{
            query = this.getParam(query, true)
            if(q) query = query + '&' + q
            path = path ? path : '/' + _this.route
            path = path + query
        }
        return {
            title: title ||'分享消息',
            imageUrl: img,
            path
        }
    },
    async friend(_this,content, type=2){
        // const { from, target, webViewUrl } = e
        let { title, img, query={}, api='share', param, q, path } = content || {}
        if(!img){
            const { id } = $store.getUser()
            const d = await $http.pull(null, api, { param: { type, ...param } })
            if( d == null ){
                return null
            }
            title = d.title
            img = d.image
            query = d.query
            path = d.path ? d.path : '/' + _this.route
            query = query + '&path=' + path
            if(id) query = query + '&share_id=' + id
            if(q) query = query + '&' + q
        }else{
            query.path =  path = path ? path : '/' + _this.route
            query = this.getParam(query, false)
            if(q) query = query + '&' + q
        }
        return {
            title: title ||'分享朋友圈',
            imageUrl: img,
            query
        }
    },
    async collect(_this,content){
        return await this.friend(_this,content, 3)
    },
    record(e){
        const { query, path } = e
        if( $fn.hasObject(query) ){
            $http.post(null,'share_record', { param:{ path, ...query } })
        }
    }
}
export default Index